/************************************************************************************
* CollectionDeleterThread.cpp                                                       *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "CollectionDeleterThread.h"

#include "database/YDb.h"

#include <QMutex>

CollectionDeleterThread::CollectionDeleterThread(
        QHash<QString, CollectorItem *>* itemMap,
        QObject *parent) :
  QThread(parent),
  m_itemMapRef(itemMap),
  m_deletionCandidates(NULL){

}



void CollectionDeleterThread::setData(QList<QString> *ids) {
    m_deletionCandidates = ids;
}

void CollectionDeleterThread::run() {
    if(m_itemMapRef != NULL &&
            m_deletionCandidates != NULL &&
            ! m_deletionCandidates->empty()) {
        YDb::get()->updater()->removeCollectionItems(m_deletionCandidates);
        QMutex mutex;
        mutex.lock();
        foreach(QString uuid, (*m_deletionCandidates)){
            CollectorItem* item = m_itemMapRef->value(uuid);
            item->setRemoved(true);
            m_itemMapRef->remove(uuid);
            delete item;
        }
        mutex.unlock();
        delete m_deletionCandidates;
        m_deletionCandidates = NULL;
    }
    emit removalComplete();
}
